
export const util = function util(arr, newarr = [], id = 1, pid = 0) {//扁平化
    if (!arr || !Array.isArray(arr) || arr.length == 0) return arr
    function toView(str) {
        if (!str) return ""
        let index = str.split("/").indexOf("views");
        let newstr = str.split("/").slice(index + 1,).join("/")
        return newstr.replace(/(.vue)?['"]\)$/g, "")
    }
    let newobj = {}
    for (let obj of arr) {
        id = newarr.length + 1
        newobj = {
            id,
            pid,
            ...obj,
            component: toView(String(obj.component)),
        }
        delete newobj.children
        newarr.push(newobj);
        if (obj.children && obj.children.length > 0) {
            util(obj.children, newarr, id, id)
        }
    }
    return newarr
}




export const tree = function tree(arr) { //反扁平化
    if (!arr || !Array.isArray(arr) || arr.length == 0) return arr

    let newarr = arr.filter(item => {
        item.children = arr.filter(v => {
            return item.id == v.pid
        })
        return !item.pid
    })

    return newarr

}


export const getMenu = (route) => {//side菜单生成
    let router = route
    function loop(router) {
        return router.filter((v) => {
            if (v.meta?.menu) {
                if (v.children&&v.children.length>0) {
                    v.children = loop(v.children);
                }
                return true;
            }
        });
    }
    return loop(router);
}


export const usedPath = function usedPath(arr, newarr = [], pathstr = "") {//常用菜单生成
    for (let item of arr) {
        if (item.children && item.children.length > 0) {
            usedPath(item.children, newarr, pathstr + item.path)
        } else {
            newarr.push({
                id:newarr.length+1,
                path: pathstr + '/' + item.path,
                menuTitle: item.meta?.menuTitle
            })
        }
        // (pathstr + '/' + item.path).split('/').slice(2).join('/')
    }
    return newarr
}